package com.yyy.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWTUtil;
import cn.hutool.log.StaticLog;
import com.yyy.bo.FrontBO;
import lombok.Data;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author HeJinYang
 * @date 2024/5/20 9:35
 */
@RestController
@CrossOrigin
public class TestController {
    @RequestMapping("/yang")
    public String ttt(@RequestBody FrontBO bo) {
        HashMap<String, String> authorizationMap = new HashMap<>();

        //feature 环境的URL
        authorizationMap.put("https://finapi-feature.sahmcapital.com/openaccount/tcc/callback",
                "apikey 9545e6ac-8310-4238-917e-5ffd17b578cc");

        //uat 环境的URL
        authorizationMap.put("https://finapi-uat.sahmcapital.com/openaccount/tcc/callback",
                "apikey 9545e6ac-8310-4238-917e-5ffd17b578cc");

        //前端：表单传递json
        StaticLog.info("bo：{}", bo);
        String formDataJson = bo.getFormData();
        String transId = bo.getTransId();
        String url = bo.getUrl();
        String authorization = authorizationMap.get(url);

        //jwt的负载
        HashMap payload = JSONUtil.toBean(formDataJson, HashMap.class);

        String key = "json.cn";
        String token = JWTUtil.createToken(payload, key.getBytes());

        StaticLog.info(token);

        //远程调用：uat的接口
        HashMap<String, Object> data = MapUtil.newHashMap();
        data.put("response", token);
        data.put("serviceName", "Login");
        data.put("status", "COMPLETED");
        data.put("transId", transId);
        String postResult = HttpRequest
                .post(url)
                .header("Content-Type", "application/json")
                .header("Authorization", authorization)
                .body(JSONUtil.toJsonStr(data))
                .execute()
                .body();

        //调用接口
        StaticLog.info("postResult：{}", postResult);

        return postResult;
    }
}
